package com.sohoometer.rsinterface;
//Java的串口开发
//MIMA集成
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

public class InterfaceDemoInput {

	public static void main(String[] args) throws Exception {
		CommPortIdentifier identifier = CommPortIdentifier.getPortIdentifier("COM1");
		CommPort open = identifier.open("Hello", 2000);
		InputStream inputStream = open.getInputStream();
		Scanner scanner = new Scanner(inputStream);
		while(scanner.hasNextByte(16)) {
			byte nextByte = scanner.nextByte();
			System.out.println(nextByte);
		}
	}
	
	   static void listPorts()
	    {
	        java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
	        while ( portEnum.hasMoreElements() ) 
	        {
	            CommPortIdentifier portIdentifier = portEnum.nextElement();
	            System.out.println(portIdentifier.getName()  +  " - " +  getPortTypeName(portIdentifier.getPortType()) );
	        }        
	    }
	    
	    static String getPortTypeName ( int portType )
	    {
	        switch ( portType )
	        {
	            case CommPortIdentifier.PORT_I2C:
	                return "I2C";
	            case CommPortIdentifier.PORT_PARALLEL:
	                return "Parallel";
	            case CommPortIdentifier.PORT_RAW:
	                return "Raw";
	            case CommPortIdentifier.PORT_RS485:
	                return "RS485";
	            case CommPortIdentifier.PORT_SERIAL:
	                return "Serial";
	            default:
	                return "unknown type";
	        }
	    }
}
